package com.yunbao.hujin.myguidepage.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;

import com.yunbao.hujin.myguidepage.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 引导页指示器 on 2017/11/30.
 * 通过{@link PageIndicatorView#initIndicator(int)}方法初始化指示器 on 2017/12/1.
 */

public class PageIndicatorView extends LinearLayout {

    private Context mContext;
    private int dotSize; // 指示器的大小（dp）
    private int margins; // 指示器间距（dp）

    private int selectDot = R.drawable.shape_oval_gray;
    private int unSelectDot = R.drawable.shape_oval_white;//选中和未选中点的背景图片

    private List<View> indicatorViews; // 存放指示器

    public PageIndicatorView(Context context) {
        super(context);
        init(context, null);
    }

    public PageIndicatorView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public PageIndicatorView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        initAttributes(context, attrs);
        this.mContext = context;
        setGravity(Gravity.CENTER);
        setOrientation(HORIZONTAL);
    }

    /**
     * 初始化 initAttributes
     */
    private void initAttributes(Context context, AttributeSet attrs) {
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.PageIndicatorView, 0, 0);
        if (attrs == null) {
            return;
        }
        try {
            dotSize = (int) array.getDimension(R.styleable.PageIndicatorView_dots_size, 10);//getResources().getDimension(R.dimen.padding_10dp)
            margins = (int) array.getDimension(R.styleable.PageIndicatorView_dots_margin, 3);
        } finally {
            array.recycle();
        }
    }

    // 设置选中点的颜色
    public void setSelectDot(int selectDot) {
        this.selectDot = selectDot;
        refreshView();
    }

    // * 设置未选中点的颜色
    public void setUnSelectDot(int unSelectDot) {
        this.unSelectDot = unSelectDot;
        refreshView();
    }

    private void refreshView() {
        invalidate();
    }

    /**
     * 初始化指示器，默认选中第一页
     * @param count  指示器数量，即页数
     */
    public void initIndicator(int count) {
        if (indicatorViews == null) {
            indicatorViews = new ArrayList<>();
        } else {
            indicatorViews.clear();
            removeAllViews();
        }
        View view;
        LayoutParams params = new LayoutParams(dotSize, dotSize);
        params.setMargins(margins, margins, margins, margins);

        for (int i = 0; i < count; i++) {
            view = new View(mContext);
            view.setBackgroundResource(unSelectDot);
            addView(view, params);
            indicatorViews.add(view);
        }
        if (indicatorViews.size() > 0) {
            indicatorViews.get(0).setBackgroundResource(selectDot);//初始页设置选中状态
        }
    }


    /**
     * 设置选中页
     * @param selected 页下标，从0开始
     */
    public void setSelectedPage(int selected) {
        for (int i = 0; i < indicatorViews.size(); i++) {
            if (i == selected)
                indicatorViews.get(i).setBackgroundResource(selectDot);
            else
                indicatorViews.get(i).setBackgroundResource(unSelectDot);
        }
    }

}
